﻿using Microsoft.AspNetCore.Components;
using MXERP.Blazor;
using MXERP.Infrastructure.Managers;
using MXERP.Shared.Wrapper;

namespace MXERP.Client.Pages.Systems
{
    public partial class Role
    {
        [Inject]
        public IRoleManager RoleManager { get; set; } = default!;
        private HashSet<RoleResponse>? SelectedItems { get; set; }
        public PageRequest<RoleRequest> Request { get; set; } = new PageRequest<RoleRequest>();
        private PageResponse<RoleResponse>? Response { get; set; }
        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            await Query();
        }
        private async Task Query()
        {
            var result = await RoleManager.Query(Request);
            Response = result.Data;
        }
        private async Task PageChange(int pageIndex)
        {
            Request.PageIndex = pageIndex;
            await Query();
        }
        private async Task Remove()
        {
            if (SelectedItems == null || SelectedItems.Count <= 0)
            {
                Snackbar.Add("请选择应用!", Severity.Error);
                return;
            }
        }
        private async Task Add()
        {
            await DialogService.ShowAsync<RoleForm>();
        }
        private async Task Edit()
        {
            if (SelectedItems == null || SelectedItems.Count <= 0)
            {
                Snackbar.Add("请选择应用!", Severity.Error);
                return;
            }
            if (SelectedItems.Count > 1)
            {
                Snackbar.Add("只能选择一个应用!", Severity.Error);
                return;
            }
            var parameters = new DialogParameters<RoleForm>
            {
                { x => x.Id, SelectedItems.First().Id },
            };
            await DialogService.ShowAsync<RoleForm>(null, parameters);
        }
    }
}
